package com.xingyi.bacs.controller;

import com.xingyi.bacs.service.ExcelToOracleService;
import com.xingyi.bacs.vo.ResponeBodyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class ExcelToOracleController {

    @Resource
    private ExcelToOracleService excelToOracleService;

    private static final Logger log = LoggerFactory.getLogger(ExcelToOracleController.class);

    @RequestMapping("/uploadByJarDeploy")
    // 定义：接收文件对象 MultipartFile file变量名要与form表单中input type="file" 标签name属性名一致
    public ResponeBodyVo uploadByJarDeploy(MultipartFile file, @RequestParam("data") String data,
        @RequestParam("tableName") String tableName, @RequestParam("seqName") String seqName) throws IOException {
        ResponeBodyVo responeBodyVo = new ResponeBodyVo();
        String fileName = file.getOriginalFilename();
        String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
        if (!"xls".equals(prefix)&&!"xlsx".equals(prefix)){
            responeBodyVo.setErrorCode(ResponeBodyVo.PARM_ERROR);
            responeBodyVo.setErrorMsg("必须传xls或者xlsx文件");
            return responeBodyVo;
        }
        // 文件名
        log.info("文件名: {}", file.getOriginalFilename());
        log.info("文件大小: {}", file.getSize());
        log.info("文件类型: {}", file.getContentType());

        //处理excel文件
        responeBodyVo = excelToOracleService.dealExcel(file, data, tableName, seqName);
        return responeBodyVo;
    }
}

